package org.csu.mypetstore.service;
import org.csu.mypetstore.domain.Category;
import org.csu.mypetstore.persistence.CategoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.csu.mypetstore.domain.Item;
import org.csu.mypetstore.domain.Product;
import org.csu.mypetstore.persistence.CategoryMapper;
import org.csu.mypetstore.persistence.ItemMapper;
import org.csu.mypetstore.persistence.ProductMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionTemplate;

import java.util.List;

@Service
public class CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;

    public Category getCategory(String categoryId) {
        return categoryMapper.getCategory(categoryId);
    }

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private ItemMapper itemMapper;

    @Autowired
    private TransactionTemplate transactionTemplate;

    public List<Category> getCategoryList() {
        return categoryMapper.getCategoryList();
    }

    public Category getCategoryById(String categoryId){return categoryMapper.getCategoryById(categoryId);}

    public Product getProductByProductId(String productId){
        return categoryMapper.getProductByProductId(productId);
    }

    public List<Product> getProductListByCategoryId(String categoryId){
        return categoryMapper.getProductListByCategoryId(categoryId);
    }

    public List<Item> getItemListByProductId(String productId){
        return categoryMapper.getItemListByProductId(productId);
    }

    public void newCategory(Category category){
        categoryMapper.newCategory(category);
    }

    public void deleteCategory(String categoryId){//事务？
        transactionTemplate.execute(new TransactionCallback() {
            public Object doInTransaction(TransactionStatus transactionStatus) {
                try {
                    System.out.println("试图删除Category，注意顺序,保证完整性约束");
                    itemMapper.deleteItemQuantityByCategory(categoryId);
                    itemMapper.deleteItemByCategory(categoryId);
                    productMapper.deleteProductByCategory(categoryId);
                    categoryMapper.deleteCategory(categoryId);
                }catch (Exception e){
                    e.printStackTrace();
                    transactionStatus.setRollbackOnly();
                }
                return null;
            }
        });

    }

    public void deleteProduct(String productId){
        transactionTemplate.execute(new TransactionCallback() {
            public Object doInTransaction(TransactionStatus transactionStatus) {
                try {
                    System.out.println("试图删除Product，注意顺序,保证完整性约束");
                    itemMapper.deleteItemQuantityByProductId(productId);
                    itemMapper.deleteItemByProductId(productId);
                    productMapper.deleteProductByProductId(productId);
                }catch (Exception e){
                    e.printStackTrace();
                    transactionStatus.setRollbackOnly();
                }
                return null;
            }
        });
    }

    public void deleteItem(String itemId){
        transactionTemplate.execute(new TransactionCallback() {
            public Object doInTransaction(TransactionStatus transactionStatus) {
                try {
                    System.out.println("试图删除Item，注意顺序,保证完整性约束");
                    itemMapper.deleteItemQuantityByItemId(itemId);
                    itemMapper.deleteItemByItemId(itemId);
                }catch (Exception e){
                    e.printStackTrace();
                    transactionStatus.setRollbackOnly();
                }
                return null;
            }
        });
    }

    public void newProduct(Product product){productMapper.newProduct(product);}

    public void newItem(Item item){itemMapper.newItem(item);}

    public void newInventory(Item item){itemMapper.newInventory(item);}

    public Item getItem(String itemId){return itemMapper.getItem(itemId);}

    public void updateItem(Item item){itemMapper.updateItem(item);}
}
